<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="primary" v-on:click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="reset">重置</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="exportExcel">导出</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="success" v-on:click="showAddDialog">设置定时任务</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="dpCpcMainTable"
            :data="tableData"
            size="small"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column label="序号" width="50">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="createTime"
                label="创建时间">
            </el-table-column>
            <el-table-column label="大众点评" align="center">
                <el-table-column
                    prop="dp3thBid"
                    width="60px"
                    label="第3位">
                </el-table-column>
                <el-table-column
                    prop="dp8thBid"
                    width="60px"
                    label="第8位">
                </el-table-column>
                <el-table-column
                    prop="dp13thBid"
                    width="60px"
                    label="第13位">
                </el-table-column>
                <el-table-column
                    prop="dp18thBid"
                    width="60px"
                    label="第18位">
                </el-table-column>
                <el-table-column
                    prop="dp23thBid"
                    width="60px"
                    label="第23位">
                </el-table-column>
                <el-table-column
                    prop="dp28thBid"
                    width="60px"
                    label="第28位">
                </el-table-column>
                <el-table-column
                    prop="dpCurrentSuggestPrice"
                    width="70px"
                    label="当前建议">
                </el-table-column>
            </el-table-column>
            <el-table-column label="美团" align="center">
                <el-table-column
                    prop="mt3thBid"
                    width="60px"
                    label="第3位">
                </el-table-column>
                <el-table-column
                    prop="mt8thBid"
                    width="60px"
                    label="第8位">
                </el-table-column>
                <el-table-column
                    prop="mt13thBid"
                    width="60px"
                    label="第13位">
                </el-table-column>
                <el-table-column
                    prop="mt18thBid"
                    width="60px"
                    label="第18位">
                </el-table-column>
                <el-table-column
                    prop="mt23thBid"
                    width="60px"
                    label="第23位">
                </el-table-column>
                <el-table-column
                    prop="mt28thBid"
                    width="60px"
                    label="第28位">
                </el-table-column>
                <el-table-column
                    prop="mtCurrentSuggestPrice"
                    width="70px"
                    label="当前建议">
                </el-table-column>
            </el-table-column>
            <el-table-column label="建议出价" align="center">
            <el-table-column
                prop="suggestDpBid"
                width="50px"
                label="点评">
            </el-table-column>
            <el-table-column
                prop="suggestMtBid"
                width="50px"
                label="美团">
            </el-table-column>
            <el-table-column
                prop="suggestBudget"
                width="60px"
                label="日预算">
            </el-table-column>
            <el-table-column
                prop="suggestBudgetFloat"
                width="80px"
                label="日预算上涨幅度">
            </el-table-column>
            <el-table-column
                prop="suggestKeywordDpBid"
                width="80px"
                label="点评关键词">
            </el-table-column>
            <el-table-column
                prop="suggestKeywordMtBid"
                width="80px"
                label="美团关键词">
            </el-table-column>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>
        <el-dialog :title=addOrUpdateDialogTitle width="30%" :visible.sync="addOrUpdateDialogVisible">
            <el-form :model="addOrUpdateForm" label-width="120px" size="small">
                <el-form-item label="门店名称">
                    <el-input v-model="addOrUpdateForm.shopName"
                              placeholder="门店名称"></el-input>
                </el-form-item>
                <el-form-item label="门店ID">
                    <el-input v-model="addOrUpdateForm.shopId"
                              placeholder="门店ID"></el-input>
                </el-form-item>
                <el-form-item label="大众点评出价列表">
                    <el-input v-model="addOrUpdateForm.dpBidList"
                              placeholder="大众点评出价列表"></el-input>
                </el-form-item>
                <el-form-item label="大众点评第3位出价">
                    <el-input v-model="addOrUpdateForm.dp3thBid"
                              placeholder="大众点评第3位出价"></el-input>
                </el-form-item>
                <el-form-item label="大众点评第8位出价">
                    <el-input v-model="addOrUpdateForm.dp8thBid"
                              placeholder="大众点评第8位出价"></el-input>
                </el-form-item>
                <el-form-item label="大众点评第13位出价">
                    <el-input v-model="addOrUpdateForm.dp13thBid"
                              placeholder="大众点评第13位出价"></el-input>
                </el-form-item>
                <el-form-item label="大众点评第18位出价">
                    <el-input v-model="addOrUpdateForm.dp18thBid"
                              placeholder="大众点评第18位出价"></el-input>
                </el-form-item>
                <el-form-item label="大众点评第23位出价">
                    <el-input v-model="addOrUpdateForm.dp23thBid"
                              placeholder="大众点评第23位出价"></el-input>
                </el-form-item>
                <el-form-item label="大众点评第28位出价">
                    <el-input v-model="addOrUpdateForm.dp28thBid"
                              placeholder="大众点评第28位出价"></el-input>
                </el-form-item>
                <el-form-item label="大众点评获取出价错误原因">
                    <el-input v-model="addOrUpdateForm.dpExceptionReason"
                              placeholder="大众点评获取出价错误原因"></el-input>
                </el-form-item>
                <el-form-item label="大众点评当前建议出价">
                    <el-input v-model="addOrUpdateForm.dpCurrentSuggestPrice"
                              placeholder="大众点评当前建议出价"></el-input>
                </el-form-item>
                <el-form-item label="美团出价列表">
                    <el-input v-model="addOrUpdateForm.mtBidList"
                              placeholder="美团出价列表"></el-input>
                </el-form-item>
                <el-form-item label="美团第3位出价">
                    <el-input v-model="addOrUpdateForm.mt3thBid"
                              placeholder="美团第3位出价"></el-input>
                </el-form-item>
                <el-form-item label="美团第8位出价">
                    <el-input v-model="addOrUpdateForm.mt8thBid"
                              placeholder="美团第8位出价"></el-input>
                </el-form-item>
                <el-form-item label="美团第13位出价">
                    <el-input v-model="addOrUpdateForm.mt13thBid"
                              placeholder="美团第13位出价"></el-input>
                </el-form-item>
                <el-form-item label="美团第18位出价">
                    <el-input v-model="addOrUpdateForm.mt18thBid"
                              placeholder="美团第18位出价"></el-input>
                </el-form-item>
                <el-form-item label="美团第23位出价">
                    <el-input v-model="addOrUpdateForm.mt23thBid"
                              placeholder="美团第23位出价"></el-input>
                </el-form-item>
                <el-form-item label="美团第28位出价">
                    <el-input v-model="addOrUpdateForm.mt28thBid"
                              placeholder="美团第28位出价"></el-input>
                </el-form-item>
                <el-form-item label="美团获取出价错误原因">
                    <el-input v-model="addOrUpdateForm.mtExceptionReason"
                              placeholder="美团获取出价错误原因"></el-input>
                </el-form-item>
                <el-form-item label="美团当前建议出价">
                    <el-input v-model="addOrUpdateForm.mtCurrentSuggestPrice"
                              placeholder="美团当前建议出价"></el-input>
                </el-form-item>
                <el-form-item label="建议点评出价">
                    <el-input v-model="addOrUpdateForm.suggestDpBid"
                              placeholder="建议点评出价"></el-input>
                </el-form-item>
                <el-form-item label="建议美团出价">
                    <el-input v-model="addOrUpdateForm.suggestMtBid"
                              placeholder="建议美团出价"></el-input>
                </el-form-item>
                <el-form-item label="建议日预算">
                    <el-input v-model="addOrUpdateForm.suggestBudget"
                              placeholder="建议日预算"
                              oninput="value=value.replace(/[^\d]/g,'')"></el-input>
                </el-form-item>
                <el-form-item label="建议日预算上涨幅度">
                    <el-input v-model="addOrUpdateForm.suggestBudgetFloat"
                              placeholder="建议日预算上涨幅度"
                              oninput="value=value.replace(/[^\d]/g,'')"></el-input>
                </el-form-item>
                <el-form-item label="建议点评关键词出价">
                    <el-input v-model="addOrUpdateForm.suggestKeywordDpBid"
                              placeholder="建议点评关键词出价"></el-input>
                </el-form-item>
                <el-form-item label="建议美团关键词出价">
                    <el-input v-model="addOrUpdateForm.suggestKeywordMtBid"
                              placeholder="建议美团关键词出价"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addOrUpdateDialogVisible = false" size="small">取 消</el-button>
                <el-button @click="addOrUpdateConfirm" type="primary" size="small">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {addDpCpcBid, deleteDpCpcBid, getDpCpcBidByPage, updateDpCpcBid} from '@/api/api'

export default {
    name: "DpCpcBid",
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {}
        }
    },
    // 这里定义需要父组件传过来的东西
    props: {
        shopId: String,
        shopName: String,
        size: {
            type: String,
            default: 'small'
        },
    },
    watch: {
        shopName: function (val, oldVal) {
            this.shopName = val;
        }
    },
    methods: {
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },

        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#dpCpcMainTable', this.shopName + 'CPC竞价数据.xlsx');
        },
        add: function () {
            addDpCpcBid(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        update: function () {
            updateDpCpcBid(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteDpCpcBid({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取表格数据
        getData: function () {
            if (this.shopId) {
                this.condition.search.shopId = this.shopId;
            }
            getDpCpcBidByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;
            });
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
